package com.knowno.customer;

import okhttp3.WebSocket;

import java.util.UUID;

public class SparkRequest {

	public static String sendRequest(String ques) {

		XfSparkWebSocketListener listener = new XfSparkWebSocketListener();
		WebSocket webSocket = null;
		String answer = null;
		try {
			webSocket = XfSparkStreamClient.sendMsg(UUID.randomUUID().toString().substring(0, 10), ques,
					listener);
			int count = 0;
			// 为了避免死循环，设置循环次数来定义超时时长
			int maxCount = 200;
			while (count <= maxCount) {
				Thread.sleep(200);
				if (listener.wsCloseFlag) {
					break;
				}
				count++;
			}
			if (count > maxCount) {
				System.out.println("大模型响应超时，请联系管理员");
			}
			answer = listener.answer.toString();
			System.out.println("StartSpark :" + answer);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			webSocket.close(1000, "");
		}

		return  answer;

	}

}
